<template>
  <div class="systemset-business-flow-container">
    <!-- <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form> -->

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="流程ID"
        prop="FlowSetID"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        label="流程环节"
        prop="FlowName"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="对应流程状态" width="200">
        <template #default="{ row }">
          {{ row.OrderFlowState }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="指定操作人" width="300">
        <template #default="{ row }">
          <el-button size="small" @click="handleAuthStaff(row)">
            指定操作人
          </el-button>
          <div
            v-for="(item, index) in row.AuthStaffs"
            :key="index"
            style="padding: 5px"
          >
            <div v-if="item.Company || item.Department">
              {{ item.Company }}-{{ item.Department }}-{{ item.StaffName }}
            </div>
            <div v-else>{{ item.StaffName }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="设置分单权限" width="200">
        <template #default="{ row }">
          <el-button size="small" @click="handleAssignOrderSet(row)">
            分单权限
          </el-button>
          <div
            v-for="(item, index) in row.AssignOrderSets"
            :key="index"
            style="padding: 5px"
          >
            {{ item.AssignOrderTargetName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row)">修改</el-button>
          <!-- <el-button text @click="handleDelete(row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
    <assigner ref="assignerRef" @fetch-data="fetchData" />
    <documents ref="documentsRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import Documents from './components/SystemsetBusinessDocuments'
  import Assigner from './components/SystemsetBusinessAssigner'
  import { getList, doDelete } from '@/api/systemsetBusinessFlow'
  import Edit from './components/SystemsetBusinessFlowEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'SystemsetBusinessFlow',
    components: { Edit, Assigner, Documents },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        documentsRef: null,
        assignerRef: null,
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
        OrderFlowStates: [
          { lable: '已签约，待到单录入', value: 1 },
          { lable: '已到单录入，待确认收款', value: 2 },
          { lable: '收款确认', value: 3 },
          { lable: '业务提单', value: 4 },
          { lable: '销售内勤审核/分单', value: 5 },
          { lable: '运营内勤分单', value: 6 },
          { lable: '店长派单', value: 7 },
          { lable: '服务中', value: 10 },
        ],
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.FlowSetID) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      // 指定操作人

      const handleAuthStaff = (row) => {
        console.log(row, '弹出指定操作人')
        state.assignerRef.showEdit(row)
      }

      // 分担权限
      const handleAssignOrderSet = (row) => {
        state.documentsRef.showEdit(row)
      }

      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleAssignOrderSet,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleAuthStaff,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
